﻿using ComLib.ConfigService;
using LogLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace QrCode
{
    public class QrCodeOption : IniConfigBase
    {
        public override string ConfigDirectory
        {
            get
            {
                string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
                directory = directory.Substring(6);
                return directory;
            }
        }

        public override string ConfigFilePath
        {
            get
            {
                if (string.IsNullOrEmpty(_strConfigPath))
                {
                    string className = this.GetType().Name;
                    _strConfigPath = Path.Combine(ConfigDirectory, className + ".ini");
                }
                return _strConfigPath;
            }
        }

        public QrCodeOption() : base()
        {
        }

        /// <summary>
        /// 波特率
        /// </summary>
        private int _baudRate = 0;
        public int BaudRate
        {
            get { return _baudRate; }
            set { _baudRate = value; }
        }

        /// <summary>
        /// 串口名称
        /// </summary>
        private string _portName = "";
        public string PortName
        {
            get { return _portName; }
            set { _portName = value; }
        }

        /// <summary>
        /// 主动扫描间隔时间（毫秒）
        /// </summary>
        private int _scanInterval = 1000;
        public int ScanInterval
        {
            get { return _scanInterval; }
            set { _scanInterval = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("BaudRate", _baudRate);
            SaveField("ScanInterval", _scanInterval);
            SaveField("PortName", _portName);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _baudRate = LoadInt32Value("BaudRate");
                _scanInterval = LoadInt32Value("ScanInterval");
                _portName = LoadStringValue("PortName");
            }
            catch (Exception ex)
            {
                string strMsg = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("二维码扫描器选项: ");
            builder.Append("[波特率]:" + _baudRate.ToString());
            builder.Append("; [主动扫描间隔时间]:" + _scanInterval);
            builder.Append("; [串口名称]:" + _portName);
            return builder.ToString();
        }

    }
}
